package com.rran.study.designpattern.behavioral.observer;

import com.rran.study.designpattern.behavioral.observer.event.app.OrderEvent;
import com.rran.study.designpattern.behavioral.observer.event.app.SmokeEvent;
import com.rran.study.designpattern.behavioral.observer.model.OrderData;
import com.rran.study.designpattern.behavioral.observer.model.Smoke;
import com.rran.study.designpattern.behavioral.observer.publisher.AppEventContext;

import java.math.BigDecimal;

/**
 * 自定义监听器，事件以及消息发送器 （脱离spring）
 * 实现同步/异步的事件发布和监听
 * 这里模拟了两个业务场景，实现了业务的解耦
 */
public class AppObserverApplication {

    public static void main(String[] args) {
        //------------------------------------
        //demo 1
        OrderData orderData = new OrderData(new BigDecimal(200),20);
        AppEventContext appEventContext = new AppEventContext();
        System.out.println("发布事件：小明同学买东西下单");
        appEventContext.publishEvent(new OrderEvent(orderData),true);

        //------------------------------------
        //demo 2
        Smoke smoke = new Smoke(20,"中华");
        AppEventContext appEventContext2 = new AppEventContext();
        System.out.println();
        System.out.println();
        System.out.println("发布事件：小明同学买了包烟");
        appEventContext2.publishEvent(new SmokeEvent(smoke));


        appEventContext.close();
        appEventContext2.close();
    }
}
